package com.jjb.www.other;

import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPUtils;
import com.jjb.www.BuildConfig;
import com.jjb.www.http.api.LoginApi;
import com.jjb.www.manager.IntentKey;

/**
 *    author : Android CRG
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/02
 *    desc   : App 配置管理类
 */
public final class AppConfig {
    private static AppConfig sInstance;
    /** 用户信息 */
    public static final String USER_INFO = "userInfo";
    /** 屏幕密度 */
    public static final String DPI = "dpi";
    /**
     * 用户信息
     */
    private LoginApi.Bean mUserBean;
    public static AppConfig getInstance() {
        if (sInstance == null) {
            synchronized (AppConfig.class) {
                if (sInstance == null) {
                    sInstance = new AppConfig();
                }
            }
        }
        return sInstance;
    }
    /**
     * 当前是否为调试模式
     */
    public static boolean isDebug() {
        return BuildConfig.DEBUG;
    }
    /**
     * 用户是否同意隐私权限
     */
    public static final String LOGIN_AGREE = "login_agree";
    /**
     * 获取当前构建的模式
     */
    public static String getBuildType() {
        return BuildConfig.BUILD_TYPE;
    }

    /**
     * 当前是否要开启日志打印功能
     */
    public static boolean isLogEnable() {
        return BuildConfig.LOG_ENABLE;
    }

    /**
     * 获取当前应用的包名
     */
    public static String getPackageName() {
        return BuildConfig.APPLICATION_ID;
    }

    /**
     * 获取当前应用的版本名
     */
    public static String getVersionName() {
        return BuildConfig.VERSION_NAME;
    }

    /**
     * 获取当前应用的版本码
     */
    public static int getVersionCode() {
        return BuildConfig.VERSION_CODE;
    }

    /**
     * 获取 Bugly Id
     */
    public static String getBuglyId() {
        return BuildConfig.BUGLY_ID;
    }

    /**
     * 获取服务器主机地址
     */
    public static String getHostUrl() {
        return BuildConfig.HOST_URL;
    }

    /**
     * 设置Token信息
     */
    public void setToken(String token) {
        SPUtils.getInstance().put(IntentKey.TOKEN, token);
    }

    public String getToken() {
        return SPUtils.getInstance().getString(IntentKey.TOKEN);
    }
    /**
     * 用户信息
     */
    public void setUserBean(LoginApi.Bean bean) {
        this.mUserBean = bean;
        SPUtils.getInstance().put(USER_INFO,GsonUtils.toJson(bean));
    }

    public LoginApi.Bean getUserBean() {
        if (mUserBean == null) {
            String userBeanJson = SPUtils.getInstance().getString(USER_INFO);
            if (!TextUtils.isEmpty(userBeanJson)) {
                mUserBean = GsonUtils.fromJson(userBeanJson, LoginApi.Bean.class);
            }
        }
        return mUserBean;
    }


    public void setDensityDpi(double dpi){
        SPUtils.getInstance().put(DPI,dpi+"");
    }
    public double getDensityDpi(){
      return   new Double(SPUtils.getInstance().getString(DPI,1.0+""));
    }

    /**
     * 清除登录信息
     */
    public void clearLoginInfo() {
        SPUtils.getInstance().remove(IntentKey.TOKEN);
        SPUtils.getInstance().remove(USER_INFO);
    }
}